/**
 * @Title: indicators_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月29日 上午8:45:59
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	
	//医疗费用
	app.controller('TotalCastCtrl',['$scope','SettleAnalyseChartService','SettleAnalyseService',
	                                function($scope,SettleAnalyseChartService,SettleAnalyseService){
		
		/*********************************************************
		 * 
		 * Scope & Variables Init
		 * 
		 */
		
		var colors = ['#dd4814', '#DD5E31', '#FF855A','yellow','#006397','#3ca5d0','#86d3e4','green'];
		var colors2 = ['#dd4814','#006397','#3ca5d0','#86d3e4','#aaa'];
		var lineSeries = [],
			barSeries = [];
		
		$scope.barOption = {
			grid:{
				y:10
			},
			series:[],
			xAxis : [{
	             type : 'category',
	             data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
	         }],
	         color:colors
		};
		
		$scope.lineOption = {
			grid:{
				y:10
			},
			xAxis : [{
	             type : 'category',
	             boundaryGap:false,
	             data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
	         }],
	         series:[],
	         color:colors2
		};
		
		
		/*********************************************************
		 * 
		 * Private function
		 * 
		 */
		
		function change2LegendData(arr, seriesArr) {
			var resArray = [];
			var ci = 0;
			$.each(arr, function(i, d){
				resArray.push({
					data:d,
					title:d.name,
					items:[{
						type:'bar',
						name:'个人现金支付',
						color:colors[ci],
						series:seriesArr[ci++]
					},{
						type:'bar',
						name:'个人帐户支付',
						color:colors[ci],
						series:seriesArr[ci++]
					},{
						type:'bar',
						name:'统筹基金支付',
						color:colors[ci],
						series:seriesArr[ci++]
					},{
						type:'bar',
						name:'医疗费用预拨',
						color:colors[ci],
						series:seriesArr[ci++]
					}]
				});
			});
			
			return resArray;
		}
		
		function change2LegendData2(arr, seriesArr) {
			var resArray = [];
			var ci = 0;
			$.each(arr, function(i, d){
				resArray.push({
					data:d,
					title:d.name,
					items:[{
						name:'费用占比',
						type:'line',
						color:colors2[ci++],
						series:seriesArr[i]
					}]
				});
			});
			
			return resArray;
		}
		
		function checkedChange(arr) {
			var orgIds = [];
			$.each(arr, function(){
				orgIds.push(this.id);
			});
			var ci = 0, cj = 0;
			SettleAnalyseChartService.getOrgTotalCostChartData(orgIds).
				success(function(res){
					lineSeries = [];
					barSeries = [];
					$.each(res, function(){
						lineSeries.push({
							type:'line',
							itemStyle: {
								normal: {
//									areaStyle: {
//										type: 'default',
//										color:colors2[cj]
//									},
									color:colors2[cj++]
								}
							},
							name:this.name,
							data:this.proportion
						});
						
						barSeries.push({
							type:'bar',
							name:this.name +' 现金支付',
							stack:this.name,
							data:this.cash,
							itemStyle: {normal: {color: colors[ci++]}}
						});
						barSeries.push({
							type:'bar',
							name:this.name +' 帐户支付',
							stack:this.name,
							data:this.account,
							itemStyle: {normal: {color: colors[ci++]}}
						});
						barSeries.push({
							type:'bar',
							name:this.name + ' 基金支付',
							stack:this.name,
							data:this.fund,
							itemStyle: {normal: {color: colors[ci++]}}
						});
						barSeries.push({
							type:'line',
							name:this.name + ' 医疗费用预拨',
							data:this.proportion,
							itemStyle: {normal: {color: colors[ci++]}}
						});
					});
					
					$scope.lineOption.series = lineSeries;
					$scope.barOption.series = barSeries;
					
					$scope.checkDatas = change2LegendData(arr, barSeries);
					$scope.checkDatas2 = change2LegendData2(arr, lineSeries);
				});
		}
		
		/*********************************************************
		 * 
		 * Event Handler
		 * 
		 */
		
		$scope.$on('fwaLegendGroup.legendStateChange', function(e, legendItem){
			
			var seriesArray, idx;
			if(legendItem.type === 'line') {
				seriesArray = $scope.lineOption.series;
			} else {
				seriesArray = $scope.barOption.series;
			}
			
			if(legendItem.hide) {
				idx = seriesArray.indexOf(legendItem.series);
				if(idx != -1) {
					seriesArray.splice(idx,1);
				} 
			} else {
				seriesArray.push(legendItem.series);
			}
		});
		
		$scope.$on('fwaLegendGroup.close',function(e, data){
			var data = SettleAnalyseService.removeFromCheckedArray({id:data.data.id});
			
			data.checked = false;
			
			checkedChange(SettleAnalyseService.checkedArray);
		});
		
		$scope.$on('settleAnalyse.dataTypeChange', function(e, type){
			checkedChange(SettleAnalyseService.checkedArray);
		});
		
		$scope.$on('settleAnalyse.rowCheckChange', function(e, arr){
			checkedChange(arr);
		});
		
	}]);
	
})();